/*
 * citygml4j - The Open Source Java API for CityGML
 * https://github.com/citygml4j
 *
 * Copyright 2013-2020 Claus Nagel <claus.nagel@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.citygml4j.model.gml.base;

import org.citygml4j.builder.copy.CopyBuilder;
import org.citygml4j.model.common.association.Associable;
import org.citygml4j.model.common.association.Association;
import org.citygml4j.model.common.base.ModelObject;
import org.citygml4j.model.common.base.ModelObjects;
import org.citygml4j.model.common.child.Child;
import org.citygml4j.model.common.child.ChildList;
import org.citygml4j.model.common.copy.Copyable;
import org.citygml4j.model.gml.GML;
import org.citygml4j.model.gml.GMLClass;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class ArrayAssociation<T extends Associable & Child> implements GML, Association<T>, Child, Copyable {
	private List<T> object;
	private Map<String, Object> localProperties;
	private ModelObject parent;

	public ArrayAssociation() {

	}
	
	public ArrayAssociation(T object) {
		addObject(object);
	}

	public ArrayAssociation(List<T> object) {
		setObject(object);
	}

	public ArrayAssociation(T[] object) {
		this(Arrays.asList(object));
	}
	
	public void addObject(T object) {
		getObject().add(object);
	}

	public List<T> getObject() {
		if (object == null)
			object = new ChildList<T>(this);

		return object;
	}

	public boolean isSetObject() {
		return object != null && !object.isEmpty();
	}

	public void setObject(List<T> object) {
		this.object = new ChildList<T>(this, object);
	}

	public void unsetObject() {
		object = ModelObjects.setNull(object);
	}

	public boolean unsetObject(T object) {
		return isSetObject() && this.object.remove(object);
	}

	public GMLClass getGMLClass() {
		return GMLClass.ARRAY_ASSOCIATION;
	}

	public Object getLocalProperty(String name) {
		if (localProperties != null)
			return localProperties.get(name);

		return null;
	}

	public void setLocalProperty(String name, Object value) {
		if (localProperties == null)
			localProperties = new HashMap<>();

		localProperties.put(name, value);
	}

	public boolean hasLocalProperty(String name) {
		return localProperties != null && localProperties.containsKey(name);
	}

	public Object unsetLocalProperty(String name) {
		if (localProperties != null)
			return localProperties.remove(name);

		return null;
	}

	public ModelObject getParent() {
		return parent;
	}

	public void setParent(ModelObject parent) {
		this.parent = parent;
	}

	public boolean isSetParent() {
		return parent != null;
	}

	public void unsetParent() {
		parent = null;
	}

	@SuppressWarnings("unchecked")
	public Object copyTo(Object target, CopyBuilder copyBuilder) {
		if (target == null)
			throw new IllegalArgumentException("Target argument must not be null for abstract copyable classes.");

		ArrayAssociation<T> copy = (ArrayAssociation<T>)target;

		if (isSetObject()) {
			for (T part : object) {
				T copyPart = (T)copyBuilder.copy(part);
				copy.addObject(copyPart);

				if (part != null && copyPart == part)
					part.setParent(this);
			}
		}

		copy.unsetParent();

		return copy;
	}

}
